package org.zfes.snowier.zbss.xzy.biz.complaint.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;

import org.zfes.snowier.zbss.xzy.biz.complaint.service.IComplaintReplayService;
import org.zfes.snowier.zbss.xzy.biz.complaint.service.IComplaintService;
import org.zfes.snowier.zbss.xzy.biz.complaint.dao.ComplaintReplayMapper;
import org.zfes.snowier.zbss.xzy.biz.complaint.model.Complaint;
import org.zfes.snowier.zbss.xzy.biz.complaint.model.ComplaintReplay;
import java.util.stream.Stream;
@Service
public class ComplaintReplayServiceImpl implements IComplaintReplayService{
 	@Autowired
	private ComplaintReplayMapper complaintReplayMapper;
 	@Autowired
	private IComplaintService complaintService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createComplaintReplay(ComplaintReplay complaintReplay) {
		ZBeanUtil.setCreateInfo(complaintReplay);
		ZBeanUtil.validateBean(complaintReplay);
		complaintReplayMapper.insertSelective(complaintReplay);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ComplaintReplay> loadComplaintReplayById(Long id) {
		if(id!=null){
			return Optional.ofNullable(complaintReplayMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateComplaintReplay(ComplaintReplay complaintReplay) {
		ZBeanUtil.setUpdatedInfo(complaintReplay);
		ZBeanUtil.validateBean(complaintReplay);
		complaintReplayMapper.updateByPrimaryKeySelective(complaintReplay);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteComplaintReplay(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		complaintReplayMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setComplaintReplayStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		complaintReplayMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadComplaintReplayDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(complaintReplayMapper.selectListCount(pm), complaintReplayMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SelectVo> loadComplaintReplaySelectVo(String keywords) {
		if(ZStrUtil.hasText(keywords)) {
			
		}
		List<SelectVo> voList=complaintReplayMapper.selectDropList(Byte.valueOf("1"));
		
		return voList==null?Lists.newArrayList():voList;
	}
    
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void replayComplaint(Long userId,String account, Long complaintId, String content) {
		ZAssert.notNull(complaintId, "请选择要操作的数据");
		ZAssert.hasText(content, "请输入回复内容");
		complaintService.loadComplaintById(complaintId).ifPresent(c->{
			createComplaintReplay(new ComplaintReplay().setConsultationId(complaintId).setReplayUserId(userId)
					.setReplayAccount(account).setContent(content));
		});
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComplaintReplay> loadByComplaintId(Long complaintId){
		if(complaintId==null) {
			return Lists.newArrayList();
		}
		return complaintReplayMapper.selectByComplaintId(complaintId);
	}
	
}